This post is all about the solution of the D. DSU Master Codeforces Round 858 (Div. 2) Problem solution.

D. DSU Master Codeforces Round 858 (Div. 2) Problem solution

Codeforces

Problem Statement :

You are given an integer n and an array an of length n1 with either 0 or 1 elements.

Let us define the value of a permutation p of length n1 using the procedure below.

Allow G to be a graph with n vertices labelled from 1 to n and no edges. Perform the following operations for each I from 1 to n1:

u and v are the (unique) vertices in the weakly connected components that contain vertices pi and pi+1 with only incoming edges;
If api=0, add a directed edge from vertex v to u; otherwise, if api=1, add a directed edge from vertex u to v.
It should be noted that after each step, it can



Format of Input :

Timofey visited a well-known summer school and discovered a tree with n vertices. A tree is an undirected connected graph with no cycles.
Except for c0, every vertex in this tree is white. The vertex c0 is black in colour.
Timofey wishes to make all of the vertices of this tree black. He uses n1 operations to accomplish this. During the i-th operation, he selects the currently white vertex ci and paints it black.
Let us define tree positivity as the shortest distance between all pairs of different black vertices in it. The number of edges on the path from v to u is the distance between the vertices v and u.
Timofey wants to know the current state of affairs after each operation.

Format of the Output :

Output the shortest distance between p and a good q for each test case.



Also Check this out:-

A. Walking Master Codeforces Round 858 (Div. 2) Problem solution, Click Here
B. Mex Master Codeforces Round 858 (Div. 2) Problem solution, Click Here
C. Sequence Master Codeforces Round 858 (Div. 2) Problem solution, Click Here
D. DSU Master Codeforces Round 858 (Div. 2) Problem solution, Click Here
Solution of the Problem: Click Here